package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerDto;
import com.qk.dto.customer.CustomerListDto;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;




    @Override
    public Customer getCustomerInfoById(Integer id) {
        Customer customer = this.baseMapper.selectById(id);
        return customer;
    }

    @Override
    public void addCustomer(CustomerDto dto) {
        if(ObjectUtil.isEmpty(dto.getName())|| ObjectUtil.isEmpty(dto.getPhone())|| ObjectUtil.isEmpty(dto.getCourseId())){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }

        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());

        this.baseMapper.insert(customer);

    }

    @Override
    public PageResult list(CustomerListDto dto) {

        Page page = new Page<>(dto.getPage(), dto.getPageSize());

        Page<CustomerDO> pageData = this.baseMapper.selectByPageMp(page,dto);

        return PageResult.<CustomerDO>builder()
                .total(Long.valueOf(pageData.getTotal()).intValue())
                .rows(pageData.getRecords())
                .build();
    }
}
